define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_publish_more, GoDialog,$stateParams) {
		if($scope.suitModel==undefined) $scope.suitModel = new Object();
		$scope.suitModel.goods_id = $stateParams.goods_id;//接受主商品的id
		$scope.suitModel.name = '';
		$scope.suitModel.min_count = '';
		$scope.suitModel.max_count = '';
		$scope.suitModel.privilege_type = 2;//默认“打折”
		$scope.suitModel.privilege = "0";
		$scope.suitModel.suitGoods = new Array();//配件数组
		$scope.num = 0;//通过其来控制数组的长度，即下标
		$scope.suitName = new Array();//显示用
		//添加
		$scope.continueAdd = function(){//数组长度加1
			if(!$scope.suitModel.suitGoods[$scope.num]){
				GoDialog.toast("请选择配件！");
			}else{
				$scope.num++;
			}
		}
		//减少
		$scope.reduce = function(index){
			$scope.suitModel.suitGoods.splice(index,1);//一次删除一个
			$scope.suitName.splice(index,1);//该数组也要删除一个，要不然显示出来的数据是错误的
			if($scope.num>0){
				$scope.num--;//下标相应的要减1
			}
		}
		
		//商品配件
		$scope.goodsList = "";//初始化数据列表
		$scope.init_data = function() {
			Productmanager_publish_more.productmanager_publish_more_goods(function(data) {
				$scope.goodsList = data.goods;
			});
		}
		$scope.init_data(); //必须在定义之后
		
		//显示配件名称 ，而不是id，如果id相同，则将对象的name存放到suitName数组中
		$scope.$watch("suitModel.suitGoods[num]",function(n,o){
			for (var i=0;i<$scope.goodsList.length;i++) {
				if(n==$scope.goodsList[i].id){
					$scope.suitName[$scope.num] = $scope.goodsList[i].name;
				}
			}
		})
		
		//提交数据
		$scope.edit = function() {
			if(!$scope.suitModel.name){
				GoDialog.toast("请输入配件组名称");
			}else if($scope.suitModel.suitGoods==0){
				GoDialog.toast("请选择配件！");
			}else{
				Productmanager_publish_more.productmanager_publish_more_edit($scope.suitModel.name,"",$scope.suitModel.goods_id,$scope.suitModel.min_count,$scope.suitModel.max_count,$scope.suitModel.privilege_type,$scope.suitModel.privilege,$scope.suitModel.suitGoods,function(data) {
					if(data.success==true){
						GoDialog.toast("提交成功");
						window.history.go(-1);//返回上一个页面   
					}else{
						GoDialog.toast("提交失败");
					}
				});
			}
		}
		
		
	}

	controller.$inject = ['$scope', 'Productmanager_publish_more','GoDialog','$stateParams'];
	
	return app.registerController("productmanager_publish_morectrl",controller);
});